package model

import (
	"Im/server/system/global"
	"Im/server/util"
)

type UserModel struct {
	Base
}

type User struct {
	Id            uint64 `gorm:"primaryKey;column:id;type:int(12)"`
	Username      string `gorm:"column:username;type:varchar(255);not null;"`
	Passwd        string `gorm:"column:passwd;type:varchar(64);not null"`
	Salt          string `gorm:"column:salt;type:varchar(64);not null"`
	Nickname      string `gorm:"column:nickname;type:varchar(255);not null"`
	Realname      string `gorm:"column:realname;type:varchar(255);not null"`
	Avatar        string `gorm:"column:avatar;type:varchar(255);not null"`
	Sex           uint8  `gorm:"column:sex;type:int(1);not null"`
	Age           uint8  `gorm:"column:age;type:int(3);not null"`
	Phone         string `gorm:"column:phone;type:varchar(12);not null"`
	Email         string `gorm:"column:email;type:varchar(255);not null"`
	Lastloginip   string `gorm:"column:lastloginip;type:varchar(20);not null"`
	Lastlogintime int64  `gorm:"column:lastlogintime;type:varchar(255);not null"`
	Status        uint8  `gorm:"column:status;type:int(1);not null"`
	Token         string `gorm:"column:token;type:varchar(255);not null"`
	Identify_comm string
}

func (m *UserModel) tabName() string {
	return global.JLZS_CONFIG.Mysql.Prefix + "user"
}

// 查询用户详情
func (m *UserModel) Detail(id uint64, where map[string]string) (User, error, bool) {

	var user User
	userWhere := &User{}
	if id > 0 {
		userWhere.Id = id
	} else if where["phone"] != "" {
		userWhere.Phone = where["phone"]
	}
	err := global.JLZS_DB.Where(&userWhere).Order("id DESC").Find(&user)
	if err.Error != nil {
		return user, err.Error, false
	}
	return user, err.Error, true
}

// 查询用户列表
func (m *UserModel) ListData(id uint64, where map[string]string) ([]User, error, bool) {

	var user []User
	err := global.JLZS_DB.Where(&User{Phone: where["phone"]}).Order("id DESC").Find(&user)
	if err.Error != nil {
		return user, err.Error, false
	}
	return user, err.Error, true
}

// 根据条WHERE IN查询
func (m *UserModel) UserList(wIn []uint64, last string) ([]User, error) {

	var user []User
	err := global.JLZS_DB.Find(&user, wIn)
	if err.Error != nil {
		return nil, err.Error
	}
	return user, nil
}

// 新增用户
func (m *UserModel) Auserdd(where map[string]string) bool {

	where["salt"] = util.Salt(6)
	user := User{
		Username:      where["username"],
		Passwd:        util.Passwd(0, where),
		Salt:          where["salt"],
		Nickname:      where["nickname"],
		Realname:      where["realname"],
		Sex:           util.StrToUint8(where["sex"]),
		Age:           util.StrToUint8(where["age"]),
		Phone:         where["phone"],
		Email:         where["email"],
		Lastloginip:   where["lastloginip"],
		Lastlogintime: util.Time(),
		Status:        0,
	}
	where = nil
	result := global.JLZS_DB.Create(&user)
	if result.Error != nil {
		return false
	}
	return true
}

// 陌生人
func (m *UserModel) StrangerList(where []uint64, fid string) []User {

	var user []User
	if len(where) > 0 {
		global.JLZS_DB.Table(m.tabName()).Debug().Not("id IN (?)", where).Scan(&user)
	} else {
		global.JLZS_DB.Raw("SELECT * FROM `"+m.tabName()+"` WHERE id != ?", fid).Scan(&user)
	}
	return user
}
